package com.demo.service.impl;

import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import ch.qos.logback.core.CoreConstants;

import com.demo.domain.MobileVerify;
import com.demo.mobile.MobileInfo;
import com.demo.repository.MobileVerifyRepository;
import com.demo.service.MobileVerifyService;
import com.demo.util.GetuiPushUtil;
import com.demo.util.GetuiStorePushUtil;
import com.demo.util.ProperityUtil;
import com.demo.util.StringUtil;

@Service
public class MobileVerifyServiceImpl implements MobileVerifyService{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private MobileVerifyRepository mobileVerifyRepository;
	
	@Autowired
	private ProperityUtil properityUtil; 

	@Override
	public boolean findLogin(String deviceId, String verify, String userId) {
		// TODO Auto-generated method stub
		
		String sql = "select count(*) from b_mobile_verify where device_id = ? and verify = ? and user_id = ?";
		int c = jdbcTemplate.queryForObject(sql, new Object[]{deviceId,verify,userId},Integer.class);
		if(c>0){
			return true;
		}
		return false;
	}
	
	 /**
     * 更新登录信息
     * @param mobileInfo 
     * @param deviceType 设备类型 ios or android
     * @param cid 个推cid
     * @param mutli 是否支持多终端登录
     * @return verify
     */
	@Override
    public String updateMobileVerify(MobileInfo mobileInfo,String deviceType,String cid,boolean mutli){
    	if(!mutli){//单设备登录删除其他设备的登录信息
    		MobileVerify verify = new MobileVerify();
    		verify.setUserId(mobileInfo.getUserid());
    		verify.setDeviceId(mobileInfo.getDeviceid());
    		
			mobileVerifyRepository.delete(verify);
			
			//解绑别名
			if(mobileInfo.getDeviceid().endsWith("@store")){
				if(StringUtils.isNotBlank(properityUtil.getGetui_appId()))
					GetuiPushUtil.unBindAliasAll(mobileInfo.getUserid());
			}else{
				if(StringUtils.isNotBlank(properityUtil.getGetui_appId()))
					GetuiPushUtil.unBindAliasAll(mobileInfo.getUserid());
			}
    	}
    	
    	//更新当前设备登录信息
		MobileVerify mobileVerify = new MobileVerify();
		mobileVerify.setVerify(UUID.randomUUID().toString());
		mobileVerify.setDeviceId(mobileInfo.getDeviceid());
		mobileVerify.setUserId(mobileInfo.getUserid());
		mobileVerify.setDeviceType(deviceType);
		mobileVerify.setDeviceBadge(0);
		if(StringUtils.isNotBlank(cid))mobileVerify.setCid(cid);
		//查询是否存在
		int count = mobileVerifyRepository.countByMobileVerify(mobileInfo.getDeviceid());
		if(count == 0){//无记录
			mobileVerify.setCreateTime(new Date());
			mobileVerify.setId(StringUtil.getUUID());
			mobileVerifyRepository.save(mobileVerify);
		}else{
			String sql = "update b_mobile_verify  set verify = '"+mobileVerify.getVerify()+"' , device_type = '"
					+mobileVerify.getDeviceType()+"',device_badge=0 , user_id = '"+mobileVerify.getUserId()+"'";
			if(StringUtils.isNoneBlank(cid)){
				sql+=",cid='"+cid+"'";
			}
			sql+=" where device_id ='"+mobileVerify.getDeviceId()+"'";
			jdbcTemplate.execute(sql);
		}
		
		
		//绑定个推别名为userid
		if(StringUtils.isNotBlank(cid)){
			if(mobileInfo.getDeviceid().endsWith("@store")){
				if(StringUtils.isNotBlank(properityUtil.getGetui_appId()))
					GetuiStorePushUtil.bindAlias(mobileInfo.getUserid(), cid);
			}else{
				if(StringUtils.isNotBlank(properityUtil.getGetui_appId()))
					GetuiPushUtil.bindAlias(mobileInfo.getUserid(), cid);
			}
		}
		
		return mobileVerify.getVerify();
	}
    
    /**
     * 注销
     * @param mobileInfo
     */
    @Override
    public void logout(MobileInfo mobileInfo){
    	MobileVerify mobileVerify = mobileVerifyRepository.findByDeviceId(mobileInfo.getDeviceid());
    	if(mobileVerify==null) return;
    	
    	jdbcTemplate.execute("delete from b_mobile_verify where device_id = "+mobileInfo.getDeviceid());
    	
    	//解绑个推别名
    	if(StringUtils.isNotBlank(mobileVerify.getCid())){
    		if(mobileInfo.getDeviceid().endsWith("@store")){
				if(StringUtils.isNotBlank(properityUtil.getGetui_appId()))
					GetuiStorePushUtil.unBindAlias(mobileInfo.getUserid(), mobileVerify.getCid());
			}else{
				if(StringUtils.isNotBlank(properityUtil.getGetui_appId()))
					GetuiPushUtil.unBindAlias(mobileInfo.getUserid(), mobileVerify.getCid());
			}
		}
    }




}
